<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author LYS
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <!-- <a-button type="primary" @click="handleForm({})" v-auth="'pu:arr:u8WmsPuArrH:edit'">
          <Icon icon="fluent:add-12-filled" /> {{ t('新增') }}
        </a-button> -->
        <a-button type="default" @click="handlePrintTask({})" v-auth="'pu:arr:u8WmsPuArrH:edit'">
          <Icon icon="ant-design:printer-outlined" /> {{ t('任务单打印') }}
        </a-button>
      </template>
      <!-- <template #firstColumn="{ record }">
        <a @click="handleForm({ id: record.id })">
          {{ record.id }}
        </a>
      </template> -->
    </BasicTable>
    <PrintModal @register="registerPrintModal" />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsWmsPuArrListH',
  });
</script>
<script lang="ts" setup>
  import { defineComponent } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { u8WmsPuArrHDelete, u8WmsPuArrHListData } from '/@/api/wms/pu/arr/u8WmsPuArrH';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import { useModal } from '/@/components/Modal';
  import PrintModal from '/@/components/print/printModal.vue';

  const { t } = useI18n('pu.arr.u8WmsPuArrH');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('上架任务主表管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('到货单号'),
        field: 'ccode',
        component: 'Input',
      },
      {
        label: t('采购类型'),
        field: 'cptcode',
        component: 'Input',
      },
      {
        label: t('到货日期'),
        field: 'ddate',
        component: 'DatePicker',
        componentProps: {
          format: 'YYYY-MM-DD',
          valueFormat: 'YYYY-MM-DD',
          // showTime: { format: 'HH:mm' },
        },
      },
      {
        label: t('供应商编码'),
        field: 'cvencode',
        component: 'Input',
      },
      {
        label: t('供应商名称'),
        field: 'cvenname',
        component: 'Input',
      },
      {
        label: t('制单人'),
        field: 'cmaker',
        component: 'Input',
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('到货单号'),
      dataIndex: 'ccode',
      key: 'a.ccode',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('采购类型'),
      dataIndex: 'cptcode',
      key: 'a.cptcode',
      sorter: true,
      width: 130,
      align: 'left',
      ifShow: false,
    },
    {
      title: t('到货日期'),
      dataIndex: 'ddate',
      key: 'a.ddate',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('供应商编码'),
      dataIndex: 'cvencode',
      key: 'a.cvencode',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('供应商名称'),
      dataIndex: 'cvenname',
      key: 'a.cvenname',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('制单人'),
      dataIndex: 'cmaker',
      key: 'a.cmaker',
      sorter: true,
      width: 130,
      align: 'left',
    },
  ];

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerPrintModal, { openModal: openPrintModal }] = useModal();
  const [registerTable, { reload, getSelectRows }] = useTable({
    api: u8WmsPuArrHListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    // actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
    rowSelection: {
      type: 'checkbox',
    },
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleDelete(record: Recordable) {
    const res = await u8WmsPuArrHDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  // 任务单打印
  async function handlePrintTask() {
    let arr = await getSelectRows();
    if (getSelectRows().length == 0) {
      showMessage(t('请先选择一行数据'));
      return;
    }
    const idsArr = arr.map((item) => {
      return item.id;
    });
    let params = {
      ids: idsArr.join(','),
      title: '采购到货任务单打印',
      height: '0.9',
      width: '0.9',
      fileName: '采购到货任务单',
    };
    openPrintModal(true, params);
  }

  function handleSuccess() {
    reload();
  }
</script>
